﻿using System.Web;
using NFine.Code;
using NFine.Domain.Entity.Biz;
using NFine.Domain.Entity.ProductManage;
using NFine.Domain.IRepository.ProductManage;
using NFine.Domain._04_IRepository.Biz;
using NFine.Repository.Biz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NFine.Domain.Entity.SystemManage;
using NFine.Domain.IRepository.SystemManage;
using NFine.Repository.ProductManage;
using NFine.Domain._02_ViewModel;
using NFine.Repository.SystemManage;
using NFine.Domain._03_Entity.Biz;
using NFine.Domain.Entity.SystemSecurity;
using NFine.Data;

namespace NFine.Application.Biz
{
    public class UserOrderApp
    {
        private IUserOrderRepository userOrderRepository = new UserOrderRepository();

        private IProductRepository productRepository = new ProductRepository();

        private IOrderItemRepository orderItemRepository = new OrderItemRepository();

        private IUserRepository userRepository = new UserRepository();

        private IUserDetailRepository userDetailRepository = new UserDetailRepository();

        /// <summary>
        /// 会员分红明细
        /// </summary>
        private IMemberBonusRepository memberBonusRepository = new MemberBonusRepository();
        /// <summary>
        /// 会员提现
        /// </summary>
        private IUserWithdrawalsRepository userWithdrawalsRepository = new UserWithdrawalsRepository();
        /// <summary>
        /// 积分赠送
        /// </summary>
        private IIntegralRepository integralRepository = new IntegralRepository();
        /// <summary>
        /// 中转中心品鉴积分
        /// </summary>
        private IZhongZhuanPinJianRepository zhongZhuanPinJianRepository = new ZhongZhuanPinJianRepository();


        //用户等级对应列表
        private IBonusRateRepository bonusRateRepository = new BonusRateRepository();
        //分红比例
        private IThreeBonusRepository threeBonusRepository = new ThreeBonusRepository();
        //用户属性
        private IUserPropertyRepository userPropertyRepository = new UserPropertyRepository();

        private IItemsDetailRepository itemsDetailRepository = new ItemsDetailRepository();



        /// 获取订单列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="type"></param>
        /// <param name="orderCode"></param>
        /// <param name="rName"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public List<UserOrderEntity> GetListForOrder(Pagination pagination, int type, string orderCode, string rName, string start, string end)
        {
            return GetListForOrder(pagination, type, orderCode, rName, start, end, string.Empty);
        }

        public List<UserOrderEntity> GetListForOrder(Pagination pagination, int type, string orderCode, string rName, string start, string end, string memberId)
        {
            var expression = ExtLinq.True<UserOrderEntity>();
            DateTime date = DateTime.Now;

            if (type > -3)
            {
                expression = expression.And(t => t.F_OrderStatus == type);
            }

            if (!string.IsNullOrEmpty(orderCode))
            {
                expression = expression.And(t => t.F_OrderCode.Contains(orderCode));
            }
            if (!string.IsNullOrEmpty(rName))
            {
                expression = expression.And(t => t.F_Consignee.Contains(rName));
            }
            if (!string.IsNullOrEmpty(start))
            {
                date = Convert.ToDateTime(start);
                expression = expression.And(a => a.F_OrderTime >= date);
            }
            if (!string.IsNullOrEmpty(end))
            {
                date = Convert.ToDateTime(end);
                expression = expression.And(a => a.F_OrderTime <= date);
            }
            if (!string.IsNullOrEmpty(memberId))
            {
                expression = expression.And(a => a.F_MemberId == memberId);
            }
            List<UserOrderEntity> list = userOrderRepository.FindList(expression, pagination).OrderByDescending(a => a.F_OrderTime).ToList();
            foreach (var temp in list)
            {
                temp.OrderItems=orderItemRepository.FindList("SELECT * FROM `biz_order_items` WHERE F_orderCode='" + temp.F_OrderCode + "'");
            }
            return list;

        }

        public List<UserOrderEntity> GetListForTransferCenter(Pagination pagination, int type, string orderCode,
            string rName, string start, string end, string TransferCencterId)
        {
            var expression = ExtLinq.True<UserOrderEntity>();
            DateTime date = DateTime.Now;

            //expression = expression.And(t => t.F_OrderStatus == 1 || t.F_OrderStatus == 2);

            if (type > -3)
            {
                expression = expression.And(t => t.F_OrderStatus == type);
            }

            if (!string.IsNullOrEmpty(orderCode))
            {
                expression = expression.And(t => t.F_OrderCode.Contains(orderCode));
            }
            if (!string.IsNullOrEmpty(rName))
            {
                expression = expression.And(t => t.F_Consignee.Contains(rName));
            }
            if (!string.IsNullOrEmpty(start))
            {
                date = Convert.ToDateTime(start);
                expression = expression.And(a => a.F_OrderTime >= date);
            }
            if (!string.IsNullOrEmpty(end))
            {
                date = Convert.ToDateTime(end);
                expression = expression.And(a => a.F_OrderTime <= date);
            }
            if (!string.IsNullOrEmpty(TransferCencterId))
            {
                expression = expression.And(a => a.F_TransferCenterId == TransferCencterId);
            }
            List<UserOrderEntity> list = userOrderRepository.FindList(expression, pagination).OrderByDescending(a => a.F_OrderTime).ToList();
            return list;
        }

        private int GetUserLevel(List<BonusRateEntity> bonusRateList, decimal money)
        {
            int level = 0;//用户从0级会员开始
            foreach (var b in bonusRateList)
            {
                if (money >= b.F_Money)
                {
                    level = b.F_Level;
                    break;
                }
            }
            return level;
        }

        /// <summary>
        /// 订单处理方法，对于新订单则需要启用事务，对各种人员的积分修改，分红的数据进行操作
        /// </summary>
        /// <param name="id"></param>
        /// <param name="orderStatus"></param>
        ///  <returns></returns>
        public int UpdateOrderStatus(string id)
        {
            int returnvalue = 1;
            UserOrderEntity orderentity = GetForm(id);
            var orderItems = GetOrderItmes(orderentity.F_OrderCode);
            if (orderentity.F_OrderStatus == 0)
            {
                orderentity.F_OrderStatus = 1;
                //加载所有用户信息
                List<UserDetailEntity> userDetailList = userDetailRepository.FindList("select * from biz_user_detail");
                UserDetailEntity userDetail = userDetailList.Find(t => t.F_UserId == orderentity.F_MemberId);

                //用户基本信息 需要在事务进行提交修改
                UserEntity user = userRepository.FindEntity(orderentity.F_MemberId);
                if (user != null && userDetail != null)
                {
                    decimal money = 0, moneyPP = 0;
                    //用户分红记录
                    MemberBonusEntity momberBonuserP = null;
                    MemberBonusEntity momberBonuserPP = null;
                    //找到父级用户
                    UserDetailEntity userP = userDetailList.Find(t => t.F_UserId == userDetail.F_ParentId);
                    //找到祖父级用户
                    UserDetailEntity userPP = null;

                    //父级和祖父级用户的现有金额
                    decimal amountP = 0, amountPP = 0;
                    if (userP != null)
                    {
                        amountP = (decimal)memberBonusRepository.GetMemberCurAmount(userP.F_UserId);
                        userPP = userDetailList.Find(t => t.F_UserId == userP.F_ParentId);
                        if (userPP != null)
                            amountPP = (decimal)memberBonusRepository.GetMemberCurAmount(userPP.F_UserId);
                    }


                    //通过购买的商品找到用户的等级
                    var pid = orderItems[0].F_ProductId;
                    var itemDe = itemsDetailRepository.FindEntity(t => t.F_SimpleSpelling == pid);

                    if (itemDe != null)
                    {
                        var userLveve = Convert.ToInt32(itemDe.F_ItemCode);
                        if (userP != null)
                        {

                            //得到当前用户的角色
                            if (userLveve == 1)
                            {
                                //如果是代言人（299）
                                //父级用户的角色
                                switch (userP.F_UserLevel)
                                {
                                    case 1://代言人
                                        var listUserPch = userDetailList.FindAll(t => t.F_ParentId == userP.F_UserId);
                                        var listuserid = new List<string>();
                                        foreach (var ua in listUserPch)
                                        {
                                            listuserid.Add(ua.F_UserId);
                                        }
                                        var listUserOrder = userOrderRepository.FindList(t => t.F_OrderStatus > 0);
                                        var count = 0;
                                        foreach (var order in listUserOrder) {
                                            if (listuserid.Contains(order.F_MemberId)) {
                                                count++;
                                            }
                                        }

                                        //判断当前此父级用户的下级数量，单数则不作处理，双数则提299！
                                        if (count % 2 == 1)
                                        {
                                            money = 299;
                                        }
                                        else
                                        {
                                            money = 0;
                                        }

                                        //299代言人，没有祖父级提成关系
                                        while (userPP != null)
                                        {
                                            if (userPP.F_UserLevel > 1)
                                            {
                                                moneyPP = userPP.F_UserLevel == 2 ? 20 : 30;//默认为0;
                                                break;
                                            }
                                            else
                                            {
                                                moneyPP = 0;
                                                userPP = userDetailList.Find(t => t.F_UserId == userPP.F_ParentId);
                                            }
                                        }

                                        break;
                                    case 2://合伙人
                                        money = ((decimal)orderentity.F_TotalMoney) / 2 + 20;
                                        break;
                                    case 3://代理人
                                        money = ((decimal)orderentity.F_TotalMoney) / 2 + 30;
                                        break;
                                }
                               
                            }
                            else if (userLveve == 2)
                            {
                                //合伙人（2990）
                                switch (userP.F_UserLevel)
                                {
                                    case 1://代言人    直推合伙人：10%
                                        money = ((decimal)orderentity.F_TotalMoney) / 10;
                                        break;
                                    case 2://合伙人   直推合伙人：40%
                                        money = ((decimal)orderentity.F_TotalMoney) * 4 / 10;
                                        break;
                                    case 3://代理人   直推合伙人：50%
                                        money = ((decimal)orderentity.F_TotalMoney) * 45 / 100;
                                        break;
                                }
                                if (userPP != null)  //间推
                                {
                                    switch (userPP.F_UserLevel)
                                    {
                                        case 1://代言人 祖父级代言人没有分成
                                            moneyPP = 0;
                                            break;
                                        case 2://合伙人  间推合伙人：8% 
                                            moneyPP = ((decimal)orderentity.F_TotalMoney) * 8 / 100;
                                            break;
                                        case 3://代理人   间推合伙人：10%
                                            moneyPP = ((decimal)orderentity.F_TotalMoney) *15/ 100;
                                            break;
                                    }
                                }
                            }
                            else if (userLveve == 3)
                            {
                                //代理商(19900+2000)
                                //合伙人（2990）
                                switch (userP.F_UserLevel)
                                {
                                    case 1://代言人    直推代理商：10%
                                        money = ((decimal)orderentity.F_TotalMoney) / 10;
                                        break;
                                    case 2://合伙人   直推代理商：40%
                                        money = ((decimal)orderentity.F_TotalMoney) * 4 / 10;
                                        break;
                                    case 3://代理人   直推代理商：50%
                                        money = ((decimal)orderentity.F_TotalMoney) * 45 / 100;
                                        break;
                                }
                                if (userPP != null)  //间推
                                {
                                    switch (userPP.F_UserLevel)
                                    {
                                        case 1://代言人 祖父级代言人没有分成
                                            moneyPP = 0;
                                            break;
                                        case 2://合伙人  间推代理人：8% 
                                            moneyPP = ((decimal)orderentity.F_TotalMoney) * 8 / 100;
                                            break;
                                        case 3://代理人   间推代理人：10%
                                            moneyPP = ((decimal)orderentity.F_TotalMoney)*15 / 100;
                                            break;
                                    }
                                }
                            }
                        }
                        if (userLveve > userDetail.F_UserLevel)
                        {
                            userDetail.F_UserLevel = userLveve;
                            user.F_jihuoTime = DateTime.Now;
                            returnvalue = 1000;
                        }
                    }
                    else
                    {
                        //该商品没有对应的角色信息，所以呢，只走拿货提成
                        //首先用户需要自己是代理商，这样上级的合伙人和代理商才能拿该代理商的进货额
                        //未发现商品对应的等级，则只对代理商的进货额提成！
                        if (userDetail.F_UserLevel == 3 && userP != null)
                        {
                            if (userP.F_UserLevel == 2)
                                money = ((decimal)orderentity.F_TotalMoney) * 5 / 100;
                            else if (userP.F_UserLevel == 3)
                                money = ((decimal)orderentity.F_TotalMoney) * 6 / 100;

                            if (userPP != null)
                            {
                                if (userPP.F_UserLevel == 2)
                                    moneyPP = ((decimal)orderentity.F_TotalMoney) * 2 / 100;
                                else if (userPP.F_UserLevel == 3)
                                    moneyPP = ((decimal)orderentity.F_TotalMoney) * 3 / 100;
                            }
                        }
                    }

                    if (userP != null&&money>0)
                    {
                        momberBonuserP = new MemberBonusEntity();
                        momberBonuserP.F_Id = Common.GuId();
                        momberBonuserP.F_MemberId = userP.F_UserId;
                        momberBonuserP.F_OrderId = id;
                        momberBonuserP.F_BonusAmount = money;
                        momberBonuserP.F_BonusLevel = 0;//代表子用户提点
                        momberBonuserP.F_BonusTime = DateTime.Now;
                        momberBonuserP.F_BonusType = 0;//订单提成
                        momberBonuserP.F_CurAmount = money + amountP;
                        momberBonuserP.F_bz = string.Format("会员[{0}({1})]的提成", user.F_RealName, user.F_Account);
                        
                    }

                    if (userPP != null && moneyPP > 0)
                    {
                        momberBonuserPP = new MemberBonusEntity();
                        momberBonuserPP.F_Id = Common.GuId();
                        momberBonuserPP.F_MemberId = userPP.F_UserId;
                        momberBonuserPP.F_OrderId = id;
                        momberBonuserPP.F_BonusAmount = moneyPP;
                        momberBonuserPP.F_BonusLevel = 1;////代表子子用户提点
                        momberBonuserPP.F_BonusTime = DateTime.Now;
                        momberBonuserPP.F_BonusType = 0;//订单提成
                        momberBonuserPP.F_CurAmount = moneyPP + amountPP;
                        momberBonuserPP.F_bz = userDetail.F_UserLevel > 1 ? string.Format("会员[{0}({1})]的提成", user.F_RealName, user.F_Account) : string.Format("会员[{0}({1})]的奖金", user.F_RealName, user.F_Account);
                    }

                    //启动事务
                    using (var db = new RepositoryBase().BeginTrans())
                    {
                        if (momberBonuserP != null)
                            db.Insert(momberBonuserP);
                        if (momberBonuserPP != null)
                            db.Insert(momberBonuserPP);
                        db.Update(userDetail);
                        db.Update(orderentity);
                        db.Update(user);
                        var r1= db.Commit();
                        if (r1 > 0)
                        {
                            return returnvalue;
                        }
                        else
                            return r1;
                    }

                }
                else
                {
                    return 0;
                }
            }
            else
                return userOrderRepository.updateOrderStatus(id, 2);
        }

        /// <summary>
        /// 作废订单
        /// </summary>
        /// <param name="oid"></param>
        /// <returns></returns>
        public int ZFOrder(string oid, int state)
        {
            return userOrderRepository.updateOrderStatus(oid, state);
        }

        public int FHOrder(string oid, string kdcode, string kdnum) {
            var order = userOrderRepository.FindEntity(oid);
            order.F_TransferCenterId = kdcode;
            order.F_ProductId = kdnum;
            var status = kdcode.Equals("1") ? 3 : 2;
            if (status > order.F_OrderStatus) {
                order.F_OrderStatus = status;
            }
            
            return userOrderRepository.Update(order);
        }

        /// <summary>
        /// 获取已完成订单总数
        /// </summary>
        /// <returns></returns>
        public int GetAllCount()
        {
            return userOrderRepository.FindList("SELECT * FROM biz_user_order WHERE SendGoodsStatus=1 and OrderStatus = 2").Count();
        }

        /// <summary>
        /// 获取已完成订单总额（默认为当年）
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public decimal GetAllOrderAmount(int year = -1)
        {
            if (year == -1)
                year = DateTime.Now.Year;
            return
                userOrderRepository.FindList(
                    string.Format(
                        "SELECT * FROM biz_user_order WHERE DATE_FORMAT(OrderTime,'%Y') = {0} and SendGoodsStatus=1 and OrderStatus = 2",
                        year)).Sum(t => t.F_TotalMoney).ToDecimal();
        }

        /// <summary>
        /// 根据id获取订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserOrderEntity GetForm(string id)
        {
            UserOrderEntity userOrderEntity = userOrderRepository.FindEntity(id);
            return userOrderEntity;
        }

        public List<OrderItemEntity> GetOrderItmes(string orderId)
        {
            var expression = ExtLinq.True<OrderItemEntity>();
            expression = expression.And(t => t.F_OrderCode == orderId);
            //Pagination p = new Pagination();
            return orderItemRepository.FindList(expression);
        }
        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="parentOrder"></param>
        /// <param name="list_OrderItems"></param>
        /// <returns></returns>
        public int SubmitOrder(UserOrderEntity parentOrder, List<OrderItemEntity> list_OrderItems, OrderActionEntity actionEntity)
        {
            return userOrderRepository.SubmitOrder(parentOrder, list_OrderItems, actionEntity);
        }
        /// <summary>
        /// 更新订单信息
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public bool UpdateEntity(UserOrderEntity order)
        {
            return userOrderRepository.Update(order) > 0;
        }


        public int GetCurMonthOrderCount()
        {
            return userOrderRepository.CurMonthOrderCount();
        }

        public decimal GetCurMonthMoney()
        {
            decimal? result = userOrderRepository.CurMonthMoneyCount();
            return result == null ? 0.00M : result.Value;

        }

        public decimal GetAllMoney()
        {
            decimal? result = userOrderRepository.AllMoneyCount();
            return result == null ? 0.00M : result.Value;
        }


        /// <summary>
        /// 消费统计图表数据
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<View_UserOrderChart> GetChartList(DateTime startTime, DateTime endTime)
        {
            string strSql = string.Format("SELECT * FROM biz_user_order WHERE F_OrderStatus > 0 AND F_OrderTime >= '{0}' AND F_OrderTime < '{1}'", new DateTime(startTime.Year, 1, 1), endTime.AddDays(1));
            List<UserOrderEntity> userOrderList = userOrderRepository.FindList(strSql);//("SELECT * FROM biz_user_order WHERE BackMoneyStatus = 0 AND OrderStatus = 2 AND SendGoodsStatus = 1").ToList();

            List<View_UserOrderChart> chartList = new List<View_UserOrderChart>();
            for (DateTime dt = startTime; dt <= endTime; dt = dt.AddDays(1))
            {
                View_UserOrderChart chartItem = new View_UserOrderChart();
                chartItem.dateTime = dt;
                chartItem.allMoney = userOrderList.Where(m => m.F_OrderTime >= new DateTime(dt.Year, 1, 1) && m.F_OrderTime < dt.AddDays(1)).Sum(d => d.F_TotalMoney);
                chartItem.newMoney = userOrderList.Where(m => m.F_OrderTime >= dt && m.F_OrderTime < dt.AddDays(1)).Sum(d => d.F_TotalMoney);
                chartList.Add(chartItem);
            }

            return chartList;
        }


        public UserEntity GetUserByOrderCode(string orderCode)
        {
            var order = userOrderRepository.FindEntity(orderCode);
            if (order == null)
                return null;
            else
                return userRepository.FindEntity(order.F_MemberId);
        }


        public decimal GetUserCurMonthCosume(string userId)
        {
            return userOrderRepository.SumUserConsumeByMonth(userId, DateTime.Now);
        }

        public decimal SumAllOrderByMonth(DateTime date)
        {
            return userOrderRepository.SumAllOrderByMonth(date);
        }


        public View_Statics GetStatics(DateTime startTime, DateTime endTime)
        {
            string strSql = string.Format("SELECT * FROM biz_user_order WHERE F_OrderStatus > 0 AND F_OrderTime >= '{0}' AND F_OrderTime < '{1}'", startTime.ToString("yyyy-MM-dd HH:mm:ss"), endTime.ToString("yyyy-MM-dd HH:mm:ss"));
            List<UserOrderEntity> userOrderList = userOrderRepository.FindList(strSql);//("SELECT * FROM biz_user_order WHERE BackMoneyStatus = 0 AND OrderStatus = 2 AND SendGoodsStatus = 1").ToList();
            View_Statics vs = new View_Statics();
            foreach (var uo in userOrderList)
            {
                vs.jysum += (decimal)uo.F_TotalMoney;
                if (uo.F_IsUseIntegral)
                {
                    vs.jifensum += (decimal)uo.F_TotalMoney;
                    vs.jifenzhf += (decimal)uo.F_ProductPrice;
                }
                else
                {
                    vs.xianjinsum += (decimal)uo.F_TotalMoney;
                    vs.xianjinzhf += (decimal)uo.F_ProductPrice;
                }
            }
            string strsql = string.Format("SELECT * FROM biz_member_bonus WHERE F_BonusTime >= '{0}' AND F_BonusTime < '{1}' and F_BonusType<4", startTime.ToString("yyyy-MM-dd HH:mm:ss"), endTime.ToString("yyyy-MM-dd HH:mm:ss"));
            List<MemberBonusEntity> mbList = memberBonusRepository.FindList(strsql);
            vs.fenhongsum = (decimal)mbList.Sum(t => t.F_BonusAmount);
            vs.guanggao = mbList.Where(t => t.F_BonusType == 0).Sum(t => t.F_BonusAmount).ToDecimal();
            vs.peixun = mbList.Where(t => t.F_BonusType == 1).Sum(t => t.F_BonusAmount).ToDecimal();
            vs.laowu = mbList.Where(t => t.F_BonusType == 2).Sum(t => t.F_BonusAmount).ToDecimal();
            vs.jingli = mbList.Where(t => t.F_BonusType == 3).Sum(t => t.F_BonusAmount).ToDecimal();

            strsql = string.Format("SELECT * FROM biz_user_withdrawals WHERE F_WithdrawalsTime >= '{0}' AND F_WithdrawalsTime < '{1}'", startTime.ToString("yyyy-MM-dd HH:mm:ss"), endTime.ToString("yyyy-MM-dd HH:mm:ss"));
            List<UserWithdrawalsEntity> uw = userWithdrawalsRepository.FindList(strsql);
            vs.tixiansum = uw.Sum(t => t.F_WithdrawalsMoney).ToDecimal();
            vs.chuli = uw.Where(t => t.F_WithdrawalsStatus == 1).Sum(t => t.F_WithdrawalsMoney).ToDecimal();
            vs.weichuli = uw.Where(t => t.F_WithdrawalsStatus == 0).Sum(t => t.F_WithdrawalsMoney).ToDecimal();

            return vs;
        }

        public View_Statics GetMemberStatics(DateTime startTime, DateTime endTime)
        {
            var u = OperatorProvider.Provider.GetCurrent();
            View_Statics vs = new View_Statics();
            string strsql = string.Format("SELECT * FROM biz_member_bonus WHERE F_MemberId='{2}' AND F_BonusTime >= '{0}' AND F_BonusTime < '{1}' and F_BonusType<4", startTime.ToString("yyyy-MM-dd HH:mm:ss"), endTime.ToString("yyyy-MM-dd HH:mm:ss"), u.UserId);
            List<MemberBonusEntity> mbList = memberBonusRepository.FindList(strsql);
            vs.fenhongsum = (decimal)mbList.Sum(t => t.F_BonusAmount);
            vs.guanggao = mbList.Where(t => t.F_BonusType == 0).Sum(t => t.F_BonusAmount).ToDecimal();
            vs.peixun = mbList.Where(t => t.F_BonusType == 1).Sum(t => t.F_BonusAmount).ToDecimal();
            vs.laowu = mbList.Where(t => t.F_BonusType == 2).Sum(t => t.F_BonusAmount).ToDecimal();
            vs.jingli = mbList.Where(t => t.F_BonusType == 3).Sum(t => t.F_BonusAmount).ToDecimal();


            strsql = string.Format("SELECT * FROM biz_user_withdrawals WHERE F_UserId='{2}' AND F_WithdrawalsTime >= '{0}' AND F_WithdrawalsTime < '{1}'", startTime.ToString("yyyy-MM-dd HH:mm:ss"), endTime.ToString("yyyy-MM-dd HH:mm:ss"), u.UserId);
            List<UserWithdrawalsEntity> uw = userWithdrawalsRepository.FindList(strsql);
            vs.tixiansum = uw.Sum(t => t.F_WithdrawalsMoney).ToDecimal();
            vs.chuli = uw.Where(t => t.F_WithdrawalsStatus == 1).Sum(t => t.F_WithdrawalsMoney).ToDecimal();
            vs.weichuli = uw.Where(t => t.F_WithdrawalsStatus == 0).Sum(t => t.F_WithdrawalsMoney).ToDecimal();

            //strsql= string.Format("SELECT * FROM biz_integral WHERE (F_fromUserID='{2}' OR F_toUserID='{2}')  AND F_Date >= '{0}' AND F_Date < '{1}'", startTime.ToString("yyyy-MM-dd HH:mm:ss"), endTime.ToString("yyyy-MM-dd HH:mm:ss"), u.UserId);
            //List<IntegralEntity> intList = integralRepository.FindList(strsql);
            //vs.jysum = intList.Where(t=>t.F_fromUserID==u.UserId).Sum(t => t.F_IntegralNumber).ToDecimal();
            //vs.xianjinsum= intList.Where(t => t.F_toUserID == u.UserId).Sum(t => t.F_IntegralNumber).ToDecimal();

            return vs;
        }
        /// <summary>
        /// 根据用户ID获取最新的一条订单
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public UserOrderEntity GetLastOrderByUserID(string uid)
        {
            var list = userOrderRepository.FindList(t => t.F_MemberId == uid);
            if (list.Count > 0)
            {
                return list.OrderByDescending(t => t.F_OrderTime).First();
            }
            else
                return null;
        }

        /// <summary>
        /// 根据订单号获取订单信息
        /// </summary>
        /// <param name="OrderCode"></param>
        /// <returns></returns>
        public UserOrderEntity GetFormForOrderCode(string OrderCode)
        {
            return userOrderRepository.FindEntity(t => t.F_OrderCode == OrderCode);
        }
    }
}

